package com.atguigu.flink.chapter11;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.catalog.hive.HiveCatalog;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/10/20 15:17
 */
public class Flink12_Hive {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        
        tenv.executeSql("create table sensor(id string, ts bigint, vc int" +
                            ")with(" +
                            "   'connector' = 'filesystem', " +
                            "   'path' = 'input/sensor.txt', " +
                            "   'format' = 'csv' " +
                            ")");
        
        // 1. 创建一个 hive catalog
        HiveCatalog hive = new HiveCatalog("hive", "gmall", "input/");
        
        // 2. 注册 hive catalog
        tenv.registerCatalog("hive", hive);
        tenv.useCatalog("hive"); // 设置默认的catalog
        tenv.useDatabase("gmall");
        // 3. 使用hive catalog 从hive中读取数据
        //        tenv.sqlQuery("select * from hive.gmall.person").execute().print();
        tenv.sqlQuery("select * from sensor").execute().print();
    }
}
